package mogiksaop.qliphoth.common.inventories;

import mogiksaop.qliphoth.common.items.ModItems;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.items.ItemStackHandler;
import net.minecraftforge.items.SlotItemHandler;

public class ContainerSingleCore extends Container {

	private ItemStackHandler items = new ItemStackHandler(4);
	protected Slot soulSlot;

	public ContainerSingleCore(EntityPlayer player) {
		super();
		for (int i = 0; i < 4; ++i) {
			this.addSlotToContainer(new SlotItemHandler(items, i, 38 + i * 32, 20));
		}

		for (int i = 0; i < 3; ++i) {
			for (int j = 0; j < 9; ++j) {
				this.addSlotToContainer(new Slot(player.inventory, j + i * 9 + 9, 8 + j * 18, 51 + i * 18));
			}
		}

		for (int i = 0; i < 9; ++i) {
			this.addSlotToContainer(new Slot(player.inventory, i, 8 + i * 18, 109));
		}
		
		this.addSlotToContainer(this.soulSlot = new SlotItemHandler(items, 0, 38 + 0 * 32, 20)
	    {
	        @Override
	        public boolean isItemValid(ItemStack stack)
	        {
	        	boolean soulFlag=false;
	        	if(stack.getItem().equals(ModItems.qliphortScoutSoul))
	        	{
	        		soulFlag=true;
	        	}
	            return stack != null && soulFlag && super.isItemValid(stack);
	        }

	        @Override
	        public int getItemStackLimit(ItemStack stack)
	        {
	            return 16;
	        }
	    });
	}
	
	@Override
	public boolean canInteractWith(EntityPlayer playerIn) {
		return true;
	}

	@Override
	public ItemStack transferStackInSlot(EntityPlayer playerIn, int index) {
		Slot slot = inventorySlots.get(index);

        if (slot == null || !slot.getHasStack())
        {
            return null;
        }

        ItemStack newStack = slot.getStack(), oldStack = newStack.copy();

        boolean isMerged = false;

        if (index >=0 && index <= 3)
        {
            isMerged = mergeItemStack(newStack, 4, 40, true);
        }
        else if (index >= 4 && index < 31)
        {
            isMerged = !soulSlot.getHasStack() && newStack.getCount() <= 16 && mergeItemStack(newStack, 0, 1, false)
                    || mergeItemStack(newStack, 31, 40, false);
        }
        else if (index >= 31 && index < 40)
        {
            isMerged = !soulSlot.getHasStack() && newStack.getCount() <= 16 && mergeItemStack(newStack, 0, 1, false)
                    || mergeItemStack(newStack, 4, 31, false);
        }

        if (!isMerged)
        {
            return null;
        }

        //原教程内使用的变量stackSize为private，现可使用getCount方法调用
        //即newStack.getCount()==0
        if (newStack.getCount()==0)
        {
            slot.putStack(null);
        }
        else
        {
            slot.onSlotChanged();
        }

        slot.onTake(playerIn, newStack);

        return oldStack;
	}
}
